/**
 * config start
 */
const path = require('path');
const chalk = require('chalk');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const config = require('./config');

const { __DEV__, __PROD__ } = config.globals;
const APP_ENTRY = path.resolve(__dirname, './client/views/index.jsx');


let cssLoader = [
    {
        loader: 'style-loader',
    },
    {
        loader: 'css-loader',
        options: {
            modules: true,
            importLoaders: 2,
            localIdentName: '[name]__[local]___[hash:base64:5]'
        },
    },
    {
        loader: 'postcss-loader',
    }
];

let cssLoaderWithoutModules = [
    {
        loader: 'style-loader',
    },
    {
        loader: 'css-loader',
    },
    {
        loader: 'postcss-loader',
    }
];

let lessLoader = [
    {
        loader: 'style-loader',
    },
    {
        loader: 'css-loader',
        options: {
            modules: true,
            localIdentName: '[name]__[local]___[hash:base64:5]'
        },
    },
    {
        loader: 'postcss-loader',
    },
    {
        loader: 'less-loader',
    }
];

let lessLoaderWithoutModules = [
    {
        loader: 'style-loader',
    },
    {
        loader: 'css-loader',
    },
    {
        loader: 'postcss-loader',
    },
    {
        loader: 'less-loader',
    }
];

const getConfig = function (webpackConfig) {

    webpackConfig.entry.redux_test = ['webpack-dev-server/client?http://127.0.0.1', 'webpack/hot/only-dev-server', APP_ENTRY];

    webpackConfig.module.rules.push(
        {
            test: /\.jsx?$/,
            use: [
                'babel-loader'
            ],
        },
        {
            test: /\.less$/,
            use: lessLoader,
            exclude: [/node_modules/, /ec_modules/],
        },
        {
            test: /\.css$/,
            use: cssLoader,
            exclude: [/node_modules/, /ec_modules/],
        },
        {
            test: /\.less$/,
            use: lessLoaderWithoutModules,
            include: [/node_modules/, /ec_modules/],
        },
        {
            test: /\.css$/,
            use: cssLoaderWithoutModules,
            include: [/node_modules/, /ec_modules/],
        }
    );

    webpackConfig.output = {
        filename: 'js/[name].js',
        publicPath: '/',
    };
    webpackConfig.devtool = '#source-map';
    webpackConfig.plugins.push(
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, './client', 'index.html'),
            hash: false,
            filename: 'index.html',
            inject: 'body',
        })
    );

    return webpackConfig;
}

module.exports = {
    getConfig
};

